package br.com.caelum.lojavirtual.logic;

import java.util.List;

import org.vraptor.annotations.Component;
import org.vraptor.annotations.In;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.annotations.Out;
import org.vraptor.scope.ScopeType;

import br.com.caelum.lojavirtual.dao.DaoFactory;
import br.com.caelum.lojavirtual.modelo.Carrinho;
import br.com.caelum.lojavirtual.modelo.Cd;
import br.com.caelum.lojavirtual.modelo.Musica;
import br.com.caelum.lojavirtual.modelo.Venda;

@Component("loja")
@InterceptedBy(DaoInterceptor.class)
public class LojaLogic {
	
	private DaoFactory daoFactory;
	@In(scope=ScopeType.SESSION,required=false)
	@Out(scope=ScopeType.SESSION)
	private Carrinho carrinho = new Carrinho();
	
	private Venda venda;
	
	public LojaLogic(DaoFactory daoFactory) {
		this.daoFactory = daoFactory;
	}
	public void adiciona(Musica m) {
		Musica musica = this.daoFactory.getMusicaDao().procura(m.getId());
		this.carrinho.adicionaMusica(musica);
	}
	public void finalizaCompra(Venda venda) {
		venda.setMusicas(this.carrinho.getMusicas());
		
		this.daoFactory.beginTransaction();
		this.daoFactory.getVendaDao().adiciona(venda);
		this.daoFactory.commit();
		this.carrinho = null;
		this.venda = venda;
	}
	public void remove(Musica m) {
		Musica musica = this.daoFactory.getMusicaDao().procura(m.getId());
		this.carrinho.removeMusica(musica);
	}
	
	public void lista(){
		
	}
	public void inicio() {
	}
	
	public void formulario() {
	}

	
	public List<Cd> getCds(){
		return this.daoFactory.getCdDao().listaTudo();
	}
	public Venda getVenda() {
		return venda;
	}
	
}
